package com.zhan.ielts.web.controller;

import java.util.List;

import com.zhan.ielts.service.word.DictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.Result;
import com.zhan.frame.core.web.domain.Result.Status;
import com.zhan.ielts.domain.Paragraph;
import com.zhan.ielts.domain.ParagraphWord;
import com.zhan.ielts.domain.query.ParagraphQuery;
import com.zhan.ielts.domain.vo.ParagraphVo;
import com.zhan.ielts.service.ParagraphService;
import com.zhan.ielts.service.ParagraphWordService;

@Controller
@RequestMapping("/paragraph")
public class ParagraphController extends BaseControllerImpl<Paragraph, ParagraphQuery> {

	private Logger log = LoggerFactory.getLogger(ParagraphController.class);

	@Autowired
	private ParagraphService paragraphService;

	@Autowired
	private ParagraphWordService paragraphWordService;

	@Autowired
	private DictionaryService dictionaryService;

	@Override
	protected BaseService<Paragraph> getBaseService() {
		return paragraphService;
	}

	@Override
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView selectList(ParagraphQuery query, @PageableDefault Pageable pageable) {
		Pageable updatedPageable = new PageRequest(pageable.getPageNumber(), 10, pageable.getSort());
		Page<ParagraphVo> page = paragraphService.queryPageListCustom(query, updatedPageable);
		ModelAndView mav = new ModelAndView(path.getListViewPath(), "page", page);
		mav.addObject("query", query);
		return mav;
	}

	@RequestMapping(value = "/edit/{id}/{word}/{page}", method = RequestMethod.GET)
	public ModelAndView editCustomView(@PathVariable("id") String id, @PathVariable("word") String word,
			@PathVariable("page") String page) throws Exception {
		word = word.replace("="," ");
		ParagraphVo paragraphVo = paragraphService.editCustomView(id, word, page);
		return new ModelAndView(path.getEditViewPath(), path.getEntityName(), paragraphVo);
	}

	@RequestMapping(value = "/edit/translation/{id}/{page}", method = RequestMethod.GET)
	public ModelAndView editTranslationView(@PathVariable("id") String id, @PathVariable("page") String page) {
		ParagraphVo paragraphVo = paragraphService.queryById(id);
		paragraphVo.setPage(page);
		return new ModelAndView("paragraph/editParagraphTranslation", path.getEntityName(), paragraphVo);
	}

	@RequestMapping(value = "/editWord", method = RequestMethod.PUT)
	public ModelAndView editParagraphAndWord(ParagraphVo entity) throws Exception {
		paragraphService.editParagraphAndWord(entity);
		//String redirectPath = "";// path.getRedirectListPath()+"?subType="+entity.getSubType()+"&passage="+entity.getPassage()+"&page="+entity.getPage()+"&word="+entity.getWord();
		String redirectPath = path.getRedirectListPath() + "?articleType=" + entity.getArticleType() + "&bookNo="
				+ entity.getBookNo() + "&paperNo=" + entity.getPaperNo() + "&passageNo=" + entity.getPassageNo()
				+ "&page=" + entity.getPage();


		return new ModelAndView(redirectPath);
	}

	@RequestMapping(value = "/editTranslation", method = RequestMethod.PUT)
	public ModelAndView editTranslation(ParagraphVo entity) {
		paragraphService.editTranslation(entity);
		// String redirectPath = "";//
		// path.getRedirectListPath()+"?subType="+entity.getSubType()+"&passage="+entity.getPassage()+"&page="+entity.getPage();

		String redirectPath = path.getRedirectListPath() + "?articleType=" + entity.getArticleType() + "&bookNo="
				+ entity.getBookNo() + "&paperNo=" + entity.getPaperNo() + "&passageNo=" + entity.getPassageNo()
				+ "&page=" + entity.getPage();

		return new ModelAndView(redirectPath);
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateParagraph(@RequestParam(value = "file", required = false) MultipartFile file) {
		log.debug("文章更新开始！");
		try {
			boolean flag = true;
			if (file.isEmpty()) {
				return "article zip file can not be null.";
			} else {
				flag = paragraphService.handleParagraph(file);
				if (!flag) {
					return "fail to update article!";
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return "system error!";
		}
		log.debug("文章更新结束！");

		return "success!";
	}

	@ResponseBody
	@RequestMapping(value = "/transfer", method = RequestMethod.POST)
	public String transferParagraph(@RequestParam(value = "file", required = false) MultipartFile file) {
		log.debug("文章更新开始！");
		try {
			boolean flag = true;
			if (file.isEmpty()) {
				return "article zip file can not be null.";
			} else {
				flag = paragraphService.transferParagraph(file);
				if (!flag) {
					return "fail to update article!";
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return "system error!";
		}
		log.debug("文章更新结束！");

		return "success!";
	}

	@ResponseBody
	@RequestMapping(value = "/updateParagraphTranslation", method = RequestMethod.POST)
	public String updateParagraphTranslation(@RequestParam(value = "file", required = false) MultipartFile file) {
		log.debug("文章更新开始！");
		try {
			boolean flag = true;
			if (file.isEmpty()) {
				return "article zip file can not be null.";
			} else {
				flag = paragraphService.updateTranslation(file);
				if (!flag) {
					return "fail to update article!";
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return "system error!";
		}
		log.debug("文章更新结束！");

		return "success!";
	}

	@ResponseBody
	@RequestMapping(value = "/updateParagraphContents", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public String updateParagraphContents(String id, String paragraphContent, String paragraphContentCn, String flag) {
		log.debug("文章内容更新开始！");
		String result;
		try {
			if (StringUtils.isEmpty(flag)) {
				return "id can not be null.";
			}

			if ("single".equalsIgnoreCase(flag)) {
				if (StringUtils.isEmpty(id)) {
					return "id can not be null.";
				} else if (StringUtils.isEmpty(paragraphContent)) {
					return "paragraphContent can not be null.";
				} else if (StringUtils.isEmpty(paragraphContentCn)) {
					return "ParagraphContentCn can not be null.";
				}
			}

			result = paragraphService.updateContents(id, paragraphContent, paragraphContentCn, flag);

		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return "system error!";
		}
		log.debug("文章内容更新结束！");

		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/updateParagraphWord", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public String updateParagraphWord(String id, String paragraphContent, String paragraphContentCn, String flag) {
		log.debug("文章内容更新开始！");
		String result;
		try {
			result = paragraphService.updateWordContents();

		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return "system error!";
		}
		log.debug("文章内容更新结束！");

		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/updateWordSeqNo", method = RequestMethod.POST)
	public String updateWordSeqNo(@RequestParam(value = "file", required = false) MultipartFile file) {
		log.debug("文章更新开始！");
		try {
			boolean flag = true;
			if (file.isEmpty()) {
				return "article zip file can not be null.";
			} else {
				flag = paragraphService.updateTranslation(file);
				if (!flag) {
					return "fail to update article!";
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return "system error!";
		}
		log.debug("文章更新结束！");

		return "success!";
	}

	@ResponseBody
	@RequestMapping(value = "/addWord", method = RequestMethod.POST)
	public String addWord(ParagraphWord paragraphWord) {
		log.debug("文章更新开始！");
		String result = "";
		try {
			if (StringUtils.isEmpty(paragraphWord.getParagraphId())) {
				return "paragraphId can not be null.";
			} else if (StringUtils.isEmpty(paragraphWord.getWord())) {
				return "word can not be null.";
			} else if (StringUtils.isEmpty(paragraphWord.getWordSelected())) {
				return "selected word can not be null.";
			}

			result = paragraphService.addWord(paragraphWord);

		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return "system error!";
		}
		log.debug("文章更新结束！");

		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/minusWord", method = RequestMethod.POST)
	public String minusWord(ParagraphWord paragraphWord) {
		log.debug("删除文章单词开始！");
		String result = "";
		try {
			if (StringUtils.isEmpty(paragraphWord.getParagraphId())) {
				return "paragraphId can not be null.";
			} else if (StringUtils.isEmpty(paragraphWord.getWord())) {
				return "word can not be null.";
			}
			// else if (StringUtils.isEmpty(paragraphWord.getWordSelected())) {
			// return "selected word can not be null.";
			// }

			result = paragraphService.minusWord(paragraphWord);

		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return "system error!";
		}
		log.debug("删除文章单词结束！");

		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getType", method = RequestMethod.POST)
	public Result getAllType() {
		ParagraphQuery query = new ParagraphQuery();
		List<String> list = paragraphService.getType(query);

		return new Result(Status.OK, list);
	}

	@ResponseBody
	@RequestMapping(value = "/getPassage", method = RequestMethod.POST)
	public Result getAllPassage(String articleType) {
		ParagraphQuery query = new ParagraphQuery();
		query.setArticleType(articleType);
		List<String> list = paragraphService.getPassage(query);

		return new Result(Status.OK, list);
	}

	@ResponseBody
	@RequestMapping(value = "/getParagraphNo", method = RequestMethod.POST)
	public Result getAllParagraphNo(String articleType, Integer bookNo, Integer paperNo, Integer passageNo) {
		ParagraphQuery query = new ParagraphQuery();
		query.setArticleType(articleType);
		query.setBookNo(bookNo);
		query.setPaperNo(paperNo);
		query.setPassageNo(passageNo);
		List<Integer> list = paragraphService.getparagraphNo(query);

		return new Result(Status.OK, list);
	}

	@ResponseBody
	@RequestMapping(value = "/handleParagraphWord", method = RequestMethod.POST)
	public Result handleParagraphWord(@RequestParam(value = "file", required = false) MultipartFile file) {
		try {
			if (file.isEmpty()) {
				return new Result(Status.ERROR, "file can not be null.");
			}

			//全量导入
//			paragraphService.handleParagraphWord(file);
			
			//增量导入
			paragraphService.handleParagraphWordIncreasement(file);
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return new Result(Status.ERROR, "系统错误");
		}

		return new Result(Status.OK, "");
	}

	// ************************生词新的sentence*****************************************

	@ResponseBody
	@RequestMapping(value = "/handleSentence", method = RequestMethod.POST)
	public Result handleSentence(String subType, String passage) {
		try {
			paragraphService.handleSentence();
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return new Result(Status.ERROR, "系统错误");
		}

		return new Result(Status.OK, "");
	}

	// ************************给sentence加密*****************************************

	@ResponseBody
	@RequestMapping(value = "/encryptSentence", method = RequestMethod.POST)
	public Result encryptSentence() {
		log.debug("句子加密开始");
		try {
			paragraphService.encryptSentence();
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return new Result(Status.ERROR, "系统错误");
		}

		log.debug("句子加密结束");
		return new Result(Status.OK, "");
	}
	
	
	// ************************导入单词场景*****************************************
	@ResponseBody
	@RequestMapping(value = "/handleWordScene", method = RequestMethod.POST)
	public Result handleWordScene(@RequestParam(value = "file", required = false) MultipartFile file) {
		try {
			if (file.isEmpty()) {
				return new Result(Status.ERROR, "file can not be null.");
			}

			//全量导入
			paragraphService.handleWordScene(file);
			
			//增量导入
//			paragraphService.handleParagraphWordIncreasement(file);
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return new Result(Status.ERROR, "系统错误");
		}

		return new Result(Status.OK, "");
	}

	// ************************导入同义词*****************************************
	@ResponseBody
	@RequestMapping(value = "/handleWordSynonym", method = RequestMethod.POST)
	public Result handleWordSynonym(@RequestParam(value = "file", required = false) MultipartFile file) {
		try {
			if (file.isEmpty()) {
				return new Result(Status.ERROR, "file can not be null.");
			}

			//全量导入
			paragraphService.handleWordSynonym(file);
			
			//增量导入
//			paragraphService.handleParagraphWordIncreasement(file);
			
			paragraphService.updateWordSynonymSeqInBatch();
			
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return new Result(Status.ERROR, "系统错误");
		}

		return new Result(Status.OK, "");
	}


	// ************************导入添加时间后的文章*****************************************
	@ResponseBody
	@RequestMapping(value = "/handleParagraphWithTime", method = RequestMethod.POST)
	public Result handleParagraphWithTime(@RequestParam(value = "file", required = false) MultipartFile file) {
		try {
			if (file.isEmpty()) {
				return new Result(Status.ERROR, "file can not be null.");
			}

			//全量导入
			paragraphService.handleParagraphWithTime(file);
			
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return new Result(Status.ERROR, "系统错误");
		}

		return new Result(Status.OK, "");
	}
	
	// ************************导入扩展的句子数据*****************************************
	@ResponseBody
	@RequestMapping(value = "/handleSentenceExtend", method = RequestMethod.POST)
	public Result handleSentenceExtend(@RequestParam(value = "file", required = false) MultipartFile file) {
		try {
			if (file.isEmpty()) {
				return new Result(Status.ERROR, "file can not be null.");
			}

			//全量导入
			paragraphService.handleSentenceExtend(file);
			
//			paragraphService.updateWordSynonymSeqInBatch();
			
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("系统错误！");
			return new Result(Status.ERROR, "系统错误");
		}

		return new Result(Status.OK, "");
	}


	//************************生词新的sentence*****************************************
	
	@ResponseBody
	@RequestMapping(value = "/oneKeyUpdateSentenceRelated", method = RequestMethod.POST)
	public Result oneKeyUpdateSentenceRelated() {
		try{
//			paragraphService.handleSentence();
			paragraphService.encryptSentence();
//			paragraphWordService.handleSentenceWord();
//			paragraphService.oneKeyUpdate();
//			paragraphService.encryptSentence();
			
	} catch (Exception e) {
		e.printStackTrace();
		log.debug("系统错误！");
		return new Result(Status.ERROR, "系统错误");
	}

		return new Result(Status.OK, "");
	}


}
